package com.salon.client.widgets;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.data.BaseListLoadResult;
import com.extjs.gxt.ui.client.data.BaseListLoader;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelReader;
import com.extjs.gxt.ui.client.data.ListLoadResult;
import com.extjs.gxt.ui.client.data.LoadEvent;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.salon.client.GreetingServiceAsync;
import com.salon.client.Salon;
import com.salon.client.beans.ClientBean;
import com.salon.client.beans.IncomeMaterialsBean;
import com.salon.client.beans.MaterialBean;
import com.salon.client.beans.ParentPagingLoadResult;
import com.salon.client.tabItems.PriceTabItemPanel;

public class UsedMaterialDialog extends Dialog{
	protected ComboBox<BeanModel> combo = new ComboBox<BeanModel>();
	protected NumberField count = new NumberField();
	protected IncomeMaterialsBean bean = new IncomeMaterialsBean();
	private PriceTabItemPanel parentPanel;
	
	
	public UsedMaterialDialog(PriceTabItemPanel panel, int incomeId) {
		parentPanel = panel;
		bean.setIncomeId(incomeId);
		setHeading("Добавление товара");
		FormLayout layout = new FormLayout();
	    layout.setLabelWidth(130);
	    layout.setDefaultWidth(200);
	    setLayout(layout);
	    
	    setButtonAlign(HorizontalAlignment.CENTER);
	    setButtons(Dialog.OKCANCEL);
	    
	    setIcon(IconHelper.createStyle("user"));
	    setModal(true);
	    setBodyBorder(true);
	    setBodyStyle("padding: 8px;background: none");
	    setWidth(400);
	    setResizable(false);
	    initComponents();
	}
	
	private void initComponents() {
	    combo.setFieldLabel("Материал");
	    combo.setDisplayField("name");  
	    combo.setWidth(150);  
	    combo.setStore(getMatStore());
	    //getMatStore();
	    combo.setTypeAhead(true);  
	    combo.setTriggerAction(TriggerAction.ALL);
	    add(combo);
	    
	    count.setFieldLabel("Количество");
	    add(count);
	    
	    
	    
	    Button ok = getButtonById(Dialog.OK);
		ok.setText("ОК");
		ok.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				if (!getData()) {
					return;
				}
				((GreetingServiceAsync) Registry.get(Salon.SERVICE)).
				addUsedMaterial(bean, new AsyncCallback<IncomeMaterialsBean>() {

					@Override
					public void onFailure(Throwable caught) {
						Info.display("Не удалось", "Добавить товар");
						
					}

					@Override
					public void onSuccess(IncomeMaterialsBean result) {
						Info.display("Добавление товара", "Успешно");
						UsedMaterialDialog.this.hide();
						UsedMaterialDialog.this.parentPanel.refreshStore();
					}
					
				});
			}
			
		});
		
	    Button cancel = getButtonById(Dialog.CANCEL);
	    cancel.setText("Отмена");
	    cancel.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				UsedMaterialDialog.this.hide();
				
			}
	    });
	}
	

	
	private ListStore<BeanModel> getMatStore() {
		RpcProxy<ListLoadResult<MaterialBean>> empProxy = new RpcProxy<ListLoadResult<MaterialBean>>() {
			@Override
			protected void load(Object loadConfig,
					final AsyncCallback<ListLoadResult<MaterialBean>> callback) {
				((GreetingServiceAsync) Registry.get(Salon.SERVICE)).
				getMaterials(0, 1000, null, new AsyncCallback<ParentPagingLoadResult<MaterialBean>>() {
		
				public void onFailure(Throwable caught) {
					callback.onFailure(caught);
				}
		
				public void onSuccess(ParentPagingLoadResult<MaterialBean> result) {
					callback.onSuccess(new BaseListLoadResult<MaterialBean>(result.getData()));
				}});
			}
		};
		BaseListLoader<ListLoadResult<MaterialBean>> empLoader = new BaseListLoader<ListLoadResult<MaterialBean>>(empProxy, new BeanModelReader());   
		ListStore<BeanModel> empStore = new ListStore<BeanModel>(empLoader) {
			protected void onLoadException(LoadEvent le) {
				Info.display("Ошибка загрузки товара", "{0}",
						le.exception.getMessage());
			}
		};
		empLoader.load();
		return empStore;
	}
	
	private boolean getData() {
		if (combo.getValue() != null) {
			MaterialBean mat = combo.getValue().getBean();
			bean.setMaterialId(mat.getId());
		}
		else {
			Info.display("Выберите", "материал");
			combo.focus();
			return false;
		}
		
		if (count.getValue() != null) {
			bean.setCount(count.getValue().intValue());
		}
		else {
			Info.display("Выберите", "количество");
			count.focus();
			return false;
		}
		return true;
	}

}
